package com.silverhaze.web.util;

import java.util.Calendar;
import java.util.Date;

/**
 * User: Babanin
 * Date: 08.01.12
 * Time: 15:27
 */
public final class DateUtil {
    public static final long HOURS_IN_DAY = 24;
    public static final long MINS_IN_HOUR = 60;
    public static final long SECS_IN_MINUTE = 60;
    public static final long MSECS_IN_SECOND = 1000;

    public static final long MSECS_IN_MINUTE = MSECS_IN_SECOND * SECS_IN_MINUTE;
    public static final long MSECS_IN_HOUR = MSECS_IN_MINUTE * MINS_IN_HOUR;
    public static final long MSECS_IN_DAY = MSECS_IN_HOUR * HOURS_IN_DAY;

    private static final Calendar CALENDAR = Calendar.getInstance();

    public static boolean isSameDay(Date a, Date b) {
        Calendar calendarA = Calendar.getInstance();
        Calendar calendarB = Calendar.getInstance();

        calendarA.setTime(a);
        calendarB.setTime(b);

        return calendarA.get(Calendar.YEAR) == calendarB.get(Calendar.YEAR) &&
                calendarA.get(Calendar.DAY_OF_YEAR) == calendarB.get(Calendar.DAY_OF_YEAR);
    }


    public static Date midnight(Date date) {
        Calendar c = CALENDAR;
        c.setTime(date);

        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);

        return c.getTime();
    }

    public static Date lateMidnight(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);

        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        c.set(Calendar.MILLISECOND, 0);

        return c.getTime();
    }

    public static Date nextDay(Date date) {
        return new Date(date.getTime() + MSECS_IN_DAY);
    }

    public static Date nextWorkDay(Date date) {
        Date day = nextDay(date);

        Calendar c = Calendar.getInstance();
        c.setTime(day);

        while (c.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || c.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
            day = nextDay(day);
            c.setTime(day);
        }

        return day;
    }

    public static boolean equalsByField(Date a, Date b, int field) {
        Calendar c = CALENDAR;

        c.setTime(a);
        long aValue = c.get(field);

        c.setTime(b);
        long bValue = c.get(field);

        return aValue == bValue;
    }

    public static Date add(Date source, int field, int amount) {
        Calendar c = CALENDAR;

        c.setTime(source);
        c.add(field, amount);

        return c.getTime();
    }

    public static Date roll(Date source, int field, int amount) {
        Calendar c = CALENDAR;

        c.setTime(source);
        c.roll(field, -amount);

        return c.getTime();
    }

    public static Date date(int month, int year) {
        Calendar c = CALENDAR;

        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month);

        return c.getTime();
    }

    public static Date date(int day, int month, int year) {
        Calendar c = CALENDAR;

        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month);
        c.set(Calendar.DAY_OF_MONTH, day);

        return c.getTime();
    }

    public static Date date(int day, int month, int year, int hour, int minute) {
        Calendar c = CALENDAR;

        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month);
        c.set(Calendar.DAY_OF_MONTH, day);

        c.set(Calendar.HOUR_OF_DAY, hour);
        c.set(Calendar.MINUTE, minute);

        return c.getTime();
    }

    public static int currentMonth(){
        CALENDAR.setTime(new Date());
        return CALENDAR.get(Calendar.MONTH);
    }

    public static int currentYear(){
        CALENDAR.setTime(new Date());
        return CALENDAR.get(Calendar.YEAR);
    }
}
